package Function;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class FCHotKey {
	private String[] codekey = { "up", "down", "left", "right", "say", "lookinto", "give", "menu","Q","space","Z","X","C","I","M","enter",
	"0","1","2","3","4","5","6","7","8","9"};// 上、下、左、右、聊天、调查、给予、菜单
	private String[] keyname = { "左键", "中键", "右键", "退格", "Enter", "Shift", "Ctrl", "Alt", "Caps", "ESC", "左", "上", "右",
			"下", ",", "-", ".", "/", ";", "=", "[", "\\", "]", "Num*", "Num+", "Num-", "Num.", "Num/", "NumL", "`",
			"'" };
	private int[] codenum = { 1, 2, 3, 8, 10, 16, 17, 18, 20, 27, 32, 37, 38, 39, 40, 44, 45, 46, 47, 59, 61, 91, 92,
			93, 106, 107, 109, 110, 111, 144, 192, 222};
	private int[] defhotkey = { 87, 83, 65, 68, 70, 3, 82, 69,81,32,90,88,67,73,77,10,
	48,49,50,51,52,53,54,55,56,57,58};// 默认按键 70-F 90-Z 88-X 67-C 73-I 77-m. 10-enter
	private int[] hotkey1 = new int[defhotkey.length];// 玩家设置按键套1
	private int[] hotkey2 = new int[defhotkey.length];// 玩家设置按键套2

	public FCHotKey() {
		try {
			for (int i = 0; i < codekey.length; i++) {
				hotkey1[i] = -1;
				hotkey2[i] = -1;
			}
			File ff = new File("./data/hotkey.vw");
			if (!ff.exists())
				ff.createNewFile();
			BufferedReader re = new BufferedReader(new FileReader(ff));
			String str = re.readLine();
			while (str != null) {
				for (int i = 0; i < codekey.length; i++)
					if (str.split("=")[0].equals(codekey[i])) {
						hotkey1[i] = Integer.valueOf(str.split("=")[1]);
						hotkey2[i] = Integer.valueOf(str.split("=")[2]);
						str = re.readLine();
					}
			}
			re.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 恢复默认按键
	public void setDefHotKey() {
		for (int i = 0; i < codekey.length; i++) {
			hotkey1[i] = -1;
			hotkey2[i] = -1;
		}
	}

	// 保存按键设置至文件
	private void writeHotKey() {
		try {
			BufferedWriter wr = new BufferedWriter(new FileWriter(new File("./data/hotkey.vw")));
			for (int i = 0; i < codekey.length; i++)
				wr.write(codekey[i] + "=" + hotkey1[i] + "=" + hotkey2[i] + "\n");
			wr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 修改热键
	public void setHotKey(String keyname, int keycode, boolean isone) {
		for (int i = 0; i < codekey.length; i++) {
			if (hotkey1[i] == keycode)
				hotkey1[i] = -1;
			if (hotkey2[i] == keycode)
				hotkey2[i] = -1;
			if (codekey[i].equals(keyname))
				if (isone)
					hotkey1[i] = keycode;
				else
					hotkey2[i] = keycode;
		}
		writeHotKey();
	}

	// 根据codekey获取keyname
	public String getKeyNameOfCodeKey(String name, boolean isone) {
		for (int i = 0; i < codekey.length; i++)
			if (codekey[i].equals(name))
				return (isone) ? getKeyCharOfKeyCode((hotkey1[i] > 0) ? hotkey1[i] : defhotkey[i])
						: getKeyCharOfKeyCode(hotkey2[i]);
		return " ";
	}

	// 检测keycode的功能
	public String getCodeKeyOfKeyCode(int keycode) {
		for (int i = 0; i < codekey.length; i++)
			if (hotkey1[i] == keycode || hotkey2[i] == keycode)
				return codekey[i];
		for (int i = 0; i < codekey.length; i++)
			if (defhotkey[i] == keycode)
				return codekey[i];
		return null;
	}

	// 根据keycode获取keychar
	private String getKeyCharOfKeyCode(int num) {
		if (num >= 0) {
			if (num > 47 && num < 58)
				return String.valueOf(num - 48);
			else if (num > 64 && num < 91)
				return String.valueOf((char) num);
			else if (num > 95 && num < 106)
				return "Num" + String.valueOf(num - 96);
			else if (num > 111 && num < 124)
				return "F" + String.valueOf(num - 111);
			else
				return keyname[getNumOfKeyCode(num)];
		} else
			return " ";
	}

	// 根据keycode获取下标
	private int getNumOfKeyCode(int num) {
		for (int i = 0; i < codenum.length; i++)
			if (codenum[i] == num)
				return i;
		return 0;
	}
}
